//
//  Account.m
//  susencloud
//
//  Created by tanchao on 15/11/10.
//  Copyright © 2015年 chaoren. All rights reserved.
//

#import "Account.h"

@implementation Account
/// 单例
+ (instancetype)sharedAccount{
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc]init];
    });
    return instance;
}
- (void)setAccess_token:(NSString *)access_token
{
    _access_token = access_token;
    kSaveUserDefaults(access_token, @"access_token");
    NSLog(@"%@", kGetUserDefaults(@"access_token"));
}
// 字典转模型对象方法
- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init])
    {
        [self setValuesForKeysWithDictionary:dict];
    }
    
    return self;
}
// 字典转模型类方法
+(instancetype)accountWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}

+ (void)loginAccountWithDict:(NSDictionary *)dict{
    Account *account = [[Account sharedAccount]initWithDict:dict];
    NSLog(@"%@",account);
    if (account.access_token!=nil && ![account.access_token isEqualToString:@""]) {
        [NOTIFICATION_CENTER postNotificationName:LOGINSUCCESS object:nil];
    }
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}

- (void)setExpires_in:(NSTimeInterval)expires_in{
    _expires_in = expires_in;
    _expires_date = [NSDate dateWithTimeIntervalSinceNow:expires_in];
}
- (NSString *)description {
    NSDictionary *dict = [self dictionaryWithValuesForKeys:@[@"state",@"expires_date",@"shareFolderRootId",@"access_token",@"refresh_token",@"spaces",@"rootFolderId",@"recycleBinId",@"resource_server",@"adminprivatekey",@"roleName",@"email",@"name",@"privatekey"]];
    return [NSString stringWithFormat:@"<%@: %p> %@", self.class, self, dict];
}
@end
